package com.freewater.cargotracker.infrastructure.persistence.mybatis.converter;

import com.freewater.cargotracker.domain.model.handlingevent.HandlingEvent;
import com.freewater.cargotracker.infrastructure.persistence.mybatis.dbobject.CargoPO;
import com.freewater.cargotracker.infrastructure.persistence.mybatis.dbobject.HandlingEventPO;
import com.freewater.cargotracker.infrastructure.persistence.mybatis.dbobject.LocationPO;
import com.freewater.cargotracker.infrastructure.persistence.mybatis.dbobject.VoyagePO;

public class HandlingEventConverter {

    public static HandlingEventPO serialize(HandlingEvent handlingEvent) {
        if (handlingEvent == null) {
            return null;
        }
        HandlingEventPO handlingEventPO = new HandlingEventPO();
        handlingEventPO.setId(handlingEvent.getId());
        handlingEventPO.setCargoPO(CargoConverter.serialize(handlingEvent.getCargo()));
        handlingEventPO.setVoyagePO(VoyageConverter.serialize(handlingEvent.getVoyage()));
        handlingEventPO.setType(handlingEvent.getEventType());
        handlingEventPO.setLocation(LocationConverter.serialize(handlingEvent.getLocation()));
        handlingEventPO.setRegistrationTime(handlingEvent.getRegistrationTime());
        handlingEventPO.setCompletionTime(handlingEvent.getCompletionTime());
        handlingEventPO.setVersion(handlingEvent.getVersion());
        return handlingEventPO;
    }

    public static HandlingEvent deserialize(HandlingEventPO handlingEventPO) {
        if (handlingEventPO == null) {
            return null;
        }
        final CargoPO cargoPO = handlingEventPO.getCargoPO();
        final LocationPO locationPO = handlingEventPO.getLocation();
        final VoyagePO voyagePO = handlingEventPO.getVoyagePO();
        HandlingEvent handlingEvent = new HandlingEvent(CargoConverter.deserialize(cargoPO),
                handlingEventPO.getType(),
                LocationConverter.deserialize(locationPO),
                handlingEventPO.getRegistrationTime(),
                handlingEventPO.getCompletionTime(),
                VoyageConverter.deserialize(voyagePO));
        handlingEvent.setId(handlingEventPO.getId());
        handlingEvent.setVersion(handlingEventPO.getVersion());
        return handlingEvent;
    }
}
